package com.hz.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.hz.web.entity.MsgSendEntity;
import com.hz.web.entity.QtUserEntity;
import com.hz.web.util.HttpRequestUtil;
import com.hz.web.util.MD5Utils;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping({"MsgSendService"})
public class MsgSendController
{
  @RequestMapping({"MsgSend"})
  @ResponseBody
  public JSONObject MsgSend(MsgSendEntity entity, HttpSession session)
  {
    JSONObject resultJson = new JSONObject();
    System.out.println("qtuserinfo:"+session.getAttribute("qtuserinfo"));
    
    if (session.getAttribute("qtuserinfo") == null) {
        resultJson.put("mess", "未登录");
        return resultJson;
    }
    QtUserEntity quser= (QtUserEntity)session.getAttribute("qtuserinfo");
    
    System.out.println("qtuserinfo:"+quser.getUsername()+"!");
    if (!quser.getUsername().trim().equals("senduser")) {
        resultJson.put("mess", "无权限");
        return resultJson;
    }
    String jshm = entity.getJshm();
    String fsnr = entity.getFsnr();

   // TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
   // TimeZone.setDefault(tz);
    Date date = new Date();
   DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   
   String timestamp = format.format(date);
   System.out.println("timestamp：" + format.format(new Date(date.getTime() - 1 * 8 * 60 * 60 * 1000)));  
   timestamp=format.format(new Date(date.getTime() - 1 * 8 * 60 * 60 * 1000));
  
    
   String sign = "0635ce0ca9185cb0app_key1004formatjsonMESSAGESEND_JSON{\"messageBody\":\"" + 
      fsnr + "\"," + 
      "\"messageFrom\":\"binzhou\",\"messageProp1\":\"【滨州信用】\",\"messageToList\":\"" + jshm + "\"," + 
      "\"ownerId\":\"07B66834-3635-4592-99FF-F5291C5A39C7\",\"Remark\":\"\",\"MessageProp2\":\"\"}methodvb.smsmessage.send" + 
      "session16DAFE175660459E93792BEDC0280C8Csign_methodmd5timestamp" + timestamp + "utc_offset+00:00ver10635ce0ca9185cb0";
   
   String MESSAGESEND_JSON=new StringBuilder("{\"messageBody\":\"").append(fsnr).append("\",\"messageFrom\":\"binzhou\",")
   .append("\"messageProp1\":\"【滨州信用】\",\"messageToList\":\"").append(jshm).append("\",\"ownerId\":")
   .append("\"07B66834-3635-4592-99FF-F5291C5A39C7\",\"Remark\":\"\",\"MessageProp2\":\"\"}").toString();
   
   try
   {
			String v_MESSAGESEND_JSON = URLEncoder.encode(MESSAGESEND_JSON,"UTF-8");
			String url = "http://101.231.89.70:8080/Router.aspx?MESSAGESEND_JSON="
					+ v_MESSAGESEND_JSON
					+ "&app_key=1004&session=16DAFE175660459E93792BEDC0280C8C"
					+ "&method=vb.smsmessage.send&format=json&ver=1"
					+ "&timestamp="
					+ URLEncoder.encode(timestamp)
					+ "&utc_offset="
					+ URLEncoder.encode("+00:00")
					+ "&sign_method=md5&sign=";
			System.out.println(sign);
			sign = MD5Utils.getMD5Str(sign).toUpperCase();
			url = url + sign;
			String result = HttpRequestUtil.httpRequest(url);
			System.out.println(url);
			System.out.println(result);
			resultJson.put("row", Integer.valueOf(1));
   }
   catch(Exception e){}
   
   
  
    return resultJson;
  }
}